package top.mschen.blog.common.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.mschen.blog.common.exception.TopChenException;
import top.mschen.blog.common.utils.ReturnUtils;
import top.mschen.blog.dto.BaseResult;

import java.util.Map;

@ControllerAdvice
public class MyControllerAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyControllerAdvice.class);

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResult<Map> errorHandler(Exception e) {
        BaseResult<Map> rlt = new BaseResult<Map>();
        if (e instanceof TopChenException) {
            TopChenException cException = (TopChenException) e;
            return ReturnUtils.returnChenFail(cException);
        } else {
            LOGGER.error("[系统异常]", e);
            return ReturnUtils.returnSysFail(e);
        }
    }
}
